Updated to have more consistent look and simpler code.
Moved function to different class.