Added tests and cleaned up utility functions.