Discovering a function which would have save me 3 hours of code