A few refactors and added comments