Improved version without hacks