Refactoring mostly.
Added comments.