Added close method and done code cleaning with some minor bug fixes