Write tests; fix bugs