Bug fixes, improvements, JUnit tests