Final code. Must be tested and refactored