Refactored and got main functionality in place