Added comments, cleaned code (slightly)