Refactored. still WIP.