Complete refactoring. No more monster func.
Added missing comments.