Cleaned code a little. Added comments