Fixed a bug, cleaned up code