refactoring; based on Angel's code instead of Sundberg's