Add comments and clean up code a bit