many changes and refactories to the code