Resolved #4. Also refactored a bit.