Improved code. It's ready to be merged