added error logging; refactoring : variables renamed to danny conventions, various tweaks