Added more debug messages; significant refactoring of module pattern