Refactor tests and add regression testing