Fix bugs to pass tests