Small bug fixes, improve code quality