updated code, syntax and clean up

This version is still untested!