Refactor some stuff
Fix some bugs