Added comments and logic. Also ran somebasic tests (interactively).