minor code changes and fixes to avoid code repetition