Refactor js into a function for future resuse and restyle page

@deanhume can you review?
Ta.