lots of cleanups and refactorings. fixed test hanging problem by using filemanip library