Widen test coverage and fix bugs