misc library additions and code refactoring