Refactored and made sure tests works better. Fixed minor bugs and missed refactorization.