major cleanup of code; functions now in separate file