corrected definition slightly based on YJ's code