Split out unit and integration tests and refactor