fixed bugs + code cleanup