Refactored some library code