The algorithm part of the code