Refactoring into proper code; client side