polished up partially debugged