Add to top function and refactor code