several bug fixes and more readable code