refactoring and code cleanup

more in line with R style guidelines