code cleanup/rewrite