Some modifications taken from spark forks