Actually fixes #2 and cleans up code.