Fixed unit tests and cleaned-up the implementation.