Major refactoring. Resolved #81.