Improve code; fix bugs