fix bugs and add integration tests