Code cleanup. Added logic for checking collisions.