Modularizing and cleaning up code