add system logging, clean up code