avatar displays outside of targeted area

There is a native WordPress function to do exactly what it sounds like you’re trying to do.

You need to replace

userphoto_thumbnail($user_info, $before="", $after="", $attributes = array(width => '40', height => '40'), $default_src="")

with

get_avatar($user_info->ID,40)

Check here for your future reference – http://codex.wordpress.org/Function_Reference/get_avatar