code cleanup, add some test functions