more code cleanup, bugs fixed