Moved offset calculation into separate function to correct offset bug on resize