fixed a lot of logic bugs

added input test