Added more documentation and more logic