Added intial code from SO user razpeitia