code cleanup, working and feature complete version