Reorganized, moved some code into a separate utils file