Improved slice function