Moved location code into new function