Changed code to be a bit more generic