Some function and code I have had in other places