major code cleanup and efficiency optimization
fixed minor issues