Improve code and add periodic task