David's Changes and cleanup of code