cleaned up the code, cutting unused parts and fixed minor bugs