Improved and simplified implementation