Refactor and clean up a bit