Added the logic, made some tweaks