Revamped code to function properly