Improve sprite animation code