Refactored the code to make it more qub friendly