updated with angular code (much smoother)